package com.shuangk.majingze.coolpapa.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.shuangk.majingze.coolpapa.R;
import com.shuangk.majingze.coolpapa.contract.user.LoginContract;
import com.shuangk.majingze.coolpapa.model.User;
import com.shuangk.majingze.coolpapa.presenter.LoginPresenterImpl;
import com.shuangk.majingze.coolpapa.utils.Router;


public class LoginActivity extends AppCompatActivity implements LoginContract.View,
        View.OnClickListener {

    private LoginContract.Presenter loginPresenter;
    private ProgressDialog progressDialog;
    private EditText usernameView, passwordView;
    private Button signIn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        new LoginPresenterImpl(this);
    }

    @Override
    public void initView() {
        usernameView = (EditText) findViewById(R.id.usernameView);
        passwordView = (EditText) findViewById(R.id.passwordView);
        signIn = (Button) findViewById(R.id.signIn);
        signIn.setOnClickListener(this);
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        loginPresenter = presenter;
    }


    @Override
    public void showLoading() {
        if (progressDialog == null)
            progressDialog = ProgressDialog.show(this, "提示", "正在登录..");
        else
            progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }

    @Override
    public User getUser() {
        return new User(usernameView.getText().toString(), passwordView.getText().toString());
    }

    @Override
    public void toMainPager() {
        Router.toMainPager(this);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showMessage(String message){
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.signIn:
                loginPresenter.start();
                break;
        }
    }
}
